package net.minecraft.src;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;

import net.minecraft.server.MinecraftServer;

public class mod_LF_GenericFurnitureMain extends BaseModMp
{
	//@MLProp public static boolean allowConsoleOut = false;
	//@MLProp public static boolean generateLogs = false;

	@MLProp public static int seattestentityid = 67;

	@MLProp public static int softchairblockid = 206;
	@MLProp public static int softchairitemid = 1501;
	@MLProp public static int woodenchairblockid = 207;
	@MLProp public static int woodenchairitemid = 1502;
	@MLProp public static int woodenstandblockid = 208;
	@MLProp public static int woodenstanditemid = 1503;
	@MLProp public static int woodentable2x1blockid = 209;
	@MLProp public static int woodentable2x1itemid = 1504;
	@MLProp public static int woodentable2x2blockid = 210;
	@MLProp public static int woodentable2x2itemid = 1505;
	@MLProp public static int woodentablehighblockid = 211;
	@MLProp public static int woodentablehighitemid = 1506;
	@MLProp public static int woodenchairhighblockid = 212;
	@MLProp public static int woodenchairhighitemid = 1507;
	@MLProp public static int woodentablelowblockid = 213;
	@MLProp public static int woodentablelowitemid = 1508;
	@MLProp public static int woodentable1x1blockid = 214;
	@MLProp public static int woodentable1x1itemid = 1509;
	@MLProp public static int sofablockid = 215;
	@MLProp public static int sofaitemid = 1510;
	@MLProp public static int benchblockid = 216;
	@MLProp public static int benchitemid = 1511;
	@MLProp public static int tvoffblockid = 217;
	@MLProp public static int tvitemid = 1512;
	@MLProp public static int tvonblockid = 218;
	@MLProp public static int softchairtestid = 1513;

	@MLProp public static int chnl1_ImageSize = 64;
	@MLProp public static int chnl1_Frames = 10;
	@MLProp public static int chnl2_ImageSize = 64;
	@MLProp public static int chnl2_Frames = 10;
	@MLProp public static int chnl3_ImageSize = 64;
	@MLProp public static int chnl3_Frames = 10;
	@MLProp public static int chnl4_ImageSize = 64;
	@MLProp public static int chnl4_Frames = 10;
	@MLProp public static int chnl5_ImageSize = 64;
	@MLProp public static int chnl5_Frames = 10;
	@MLProp public static int chnl6_ImageSize = 64;
	@MLProp public static int chnl6_Frames = 10;
	@MLProp public static int chnl7_ImageSize = 64;
	@MLProp public static int chnl7_Frames = 10;
	@MLProp public static int chnl8_ImageSize = 64;
	@MLProp public static int chnl8_Frames = 10;

	@Override
	public String getVersion() 
	{
		return "1.4 for MineCraft 1.2.3";
	}

	@Override
	public void handleLogin(EntityPlayerMP player)
	{
		World world = player.worldObj;
		List<EntityPlayer> players = world.playerEntities;
		Packet230ModLoader packet = new Packet230ModLoader();

		int playerCount = 0;

		for(int i = 0; i < players.size(); i++)
		{
			if(players.get(i).ridingEntity != null && players.get(i).ridingEntity instanceof LF_EntitySeat)
				playerCount++;
		}

		if(playerCount == 0)
			return;

		packet.dataInt = new int[(playerCount*2) + 1];
		packet.dataInt[0] = 1;
		playerCount = 0;

		for(int i=0; i<players.size(); i++)
		{
			if(players.get(i).ridingEntity != null && players.get(i).ridingEntity instanceof LF_EntitySeat)
			{
				packet.dataInt[(playerCount*2) + 1] = players.get(i).entityId;
				packet.dataInt[(playerCount*2) + 2] = players.get(i).ridingEntity.entityId;
				playerCount++;
			}
		}

		ModLoaderMp.sendPacketTo(this, player, packet);
	}

	public static int getChannelImageSize(int channel) {
		switch(channel) {
			case 1:
			{
				return chnl1_ImageSize;
			}
			case 2:
			{
				return chnl2_ImageSize;
			}
			case 3:
			{
				return chnl3_ImageSize;
			}
			case 4:
			{
				return chnl4_ImageSize;
			}
			case 5:
			{
				return chnl5_ImageSize;
			}
			case 6:
			{
				return chnl6_ImageSize;
			}
			case 7:
			{
				return chnl7_ImageSize;
			}
			case 8:
			{
				return chnl8_ImageSize;
			}
			default: 
			{
				return 0;
			}
		}
	}

	public static int getChannelFrames(int channel) {
		switch(channel) {
			case 1:
			{
				return chnl1_Frames;
			}
			case 2:
			{
				return chnl2_Frames;
			}
			case 3:
			{
				return chnl3_Frames;
			}
			case 4:
			{
				return chnl4_Frames;
			}
			case 5:
			{
				return chnl5_Frames;
			}
			case 6:
			{
				return chnl6_Frames;
			}
			case 7:
			{
				return chnl7_Frames;
			}
			case 8:
			{
				return chnl8_Frames;
			}
			default: 
			{
				return 0;
			}
		}
	}

	@Override
	public void load()
	{
		setupIds();
		setupEntities();
		setupBlocks();

		System.out.println("LF - Generic Furniture + TV: Courtesy of Lord Flower/HojoINC");
	}

	public void setupEntities()
	{
		ModLoader.registerEntityID(LF_EntitySeat.class, "LF_EntitySeatTest", seattestentityid);
		ModLoaderMp.registerEntityTrackerEntry(LF_EntitySeat.class, seattestentityid);
		ModLoaderMp.registerEntityTracker(LF_EntitySeat.class, 160, 1);
	}

	public void setupBlocks()
	{
		// Removed tile entities renderers and name adding

		ModLoader.registerTileEntity(LF_TileEntitySoftChair.class, "LF_Tile_SoftChair");
		ModLoader.addRecipe(new ItemStack(itemSoftChair, 1), new Object[] {
			"www", "wpw", "ppp",
			Character.valueOf('w'),
			Block.cloth,
			Character.valueOf('p'),
			Block.planks,
			Character.valueOf('s'),
			Item.sign
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenChair.class, "LF_Tile_WoodenChair");   
		ModLoader.addRecipe(new ItemStack(itemWoodenChair, 1), new Object[] {
			"sss", "ppp", "s s",
			Character.valueOf('p'),
			Block.planks,
			Character.valueOf('s'),
			Item.stick
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTableStand.class, "LF_Tile_WoodenTableStand");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTableStand, 1), new Object[] {
			" p ", "sss",
			Character.valueOf('p'),
			Block.planks,
			Character.valueOf('s'),
			Item.stick
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTable2x1.class, "LF_Tile_WoodenTable2x1");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTable2x1, 1), new Object[] {
			"tt",
			Character.valueOf('t'),
			itemWoodenTable1x1
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTable2x2.class, "LF_Tile_WoodenTable2x2");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTable2x2, 1), new Object[] {
			"tt",
			Character.valueOf('t'),
			itemWoodenTable2x1
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTableHigh.class, "LF_Tile_WoodenTableHigh");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTableHigh, 1), new Object[] {
			"sts", "s s",
			Character.valueOf('s'),
			Item.stick,
			Character.valueOf('t'),
			itemWoodenTable1x1
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenChairHigh.class, "LF_Tile_WoodenChairHigh");   
		ModLoader.addRecipe(new ItemStack(itemWoodenChairHigh, 1), new Object[] {
			"scs", "s s",
			Character.valueOf('s'),
			Item.stick,
			Character.valueOf('c'),
			itemWoodenChair
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTableLow.class, "LF_Tile_WoodenTableLow");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTableLow, 1), new Object[] {
			"t",
			Character.valueOf('t'),
			itemWoodenTable1x1
		});

		ModLoader.registerTileEntity(LF_TileEntityWoodenTable1x1.class, "LF_Tile_WoodenTable1x1");   
		ModLoader.addRecipe(new ItemStack(itemWoodenTable1x1, 1), new Object[] {
			" t ", "sss",
			Character.valueOf('t'),
			itemWoodenTableStand,
			Character.valueOf('s'),
			Item.stick
		});

		ModLoader.registerTileEntity(LF_TileEntitySofa.class, "LF_Tile_Sofa");   
		ModLoader.addRecipe(new ItemStack(itemSofa, 1), new Object[] {
			"ss",
			Character.valueOf('s'),
			itemSoftChair
		});

		ModLoader.registerTileEntity(LF_TileEntityBench.class, "LF_Tile_Bench");   
		ModLoader.addRecipe(new ItemStack(itemBench, 1), new Object[] {
			"cc",
			Character.valueOf('c'),
			itemWoodenChair
		});

		ModLoader.registerTileEntity(LF_TileEntityTV.class, "LF_Tile_TV");   
		ModLoader.addRecipe(new ItemStack(itemTV, 1), new Object[] {
			"ppp","prp","ggg",
			Character.valueOf('p'),
			Block.planks,
			Character.valueOf('r'),
			Item.redstone,
			Character.valueOf('g'),
			Block.glass
		});
	}

	public Block makeBlock(Class blockClass, int id, Class tileClass, Material mat, float hard, String name)
	{
		try
		{
			return ((LF_BlockFurnitureBase)(blockClass.getConstructors()[0].newInstance(id,0,tileClass,mat))).setHardness(hard).setBlockName(name);
		}
		catch(Exception e)
		{
			System.err.print(e);
			return null;
		}
	}

	public Item makeItem(int id, Block block, String filename, String name)
	{
		return new LF_ItemFurniture(id, (LF_BlockFurnitureBase)block).setIconIndex(ModLoader.addOverride("/gui/items.png", "/LordFlower/Items/" + filename)).setItemName(name);
	}

	public Item makeItem(int id, String filename, String name)
	{
		return new LF_ItemSpawner(id).setIconIndex(ModLoader.addOverride("/gui/items.png", "/LordFlower/Items/" + filename)).setItemName(name);
	}

	public void setupIds()
	{
		blockSoftChair = makeBlock(LF_BlockSoftChair.class, softchairblockid, LF_TileEntitySoftChair.class, Material.cloth, 2.0f, "LF_Block_SoftChair");
		itemSoftChair = makeItem(softchairitemid, blockSoftChair, "LF_SoftChair.png", "LF_Item_SoftChair");

		blockWoodenChair = makeBlock(LF_BlockWoodenChair.class, woodenchairblockid, LF_TileEntityWoodenChair.class, Material.wood, 2.0f, "LF_Block_WoodenChair");
		itemWoodenChair = makeItem(woodenchairitemid, blockWoodenChair, "LF_WoodenChair.png", "LF_Item_WoodenChair");

		blockWoodenTableStand = makeBlock(LF_BlockWoodenTableStand.class, woodenstandblockid, LF_TileEntityWoodenTableStand.class, Material.wood, 2.0f, "LF_Block_WoodenTableStand");
		itemWoodenTableStand = makeItem(woodenstanditemid, blockWoodenTableStand, "LF_WoodenTableStand.png", "LF_Item_WoodenTableStand");

		blockWoodenTable2x1 = makeBlock(LF_BlockWoodenTable2x1.class, woodentable2x1blockid, LF_TileEntityWoodenTable2x1.class, Material.wood, 2.0f, "LF_Block_WoodenTable2x1");
		itemWoodenTable2x1 = makeItem(woodentable2x1itemid, blockWoodenTable2x1, "LF_WoodenTable2x1.png", "LF_Item_WoodenTable2x1");

		blockWoodenTable2x2 = makeBlock(LF_BlockWoodenTable2x2.class, woodentable2x2blockid, LF_TileEntityWoodenTable2x2.class, Material.wood, 2.0f, "LF_Block_WoodenTable2x2");
		itemWoodenTable2x2 = makeItem(woodentable2x2itemid, blockWoodenTable2x2, "LF_WoodenTable2x2.png", "LF_Item_WoodenTable2x2");

		blockWoodenTableHigh = makeBlock(LF_BlockWoodenTableHigh.class, woodentablehighblockid, LF_TileEntityWoodenTableHigh.class, Material.wood, 2.0f, "LF_Block_WoodenTableHigh");
		itemWoodenTableHigh = makeItem(woodentablehighitemid, blockWoodenTableHigh, "LF_WoodenTableHigh.png", "LF_Item_WoodenTableHigh");

		blockWoodenChairHigh = makeBlock(LF_BlockWoodenChairHigh.class, woodenchairhighblockid, LF_TileEntityWoodenChairHigh.class, Material.wood, 2.0f, "LF_Block_WoodenChairHigh");
		itemWoodenChairHigh = makeItem(woodenchairhighitemid, blockWoodenChairHigh, "LF_WoodenChairHigh.png", "LF_Item_WoodenChairHigh");

		blockWoodenTableLow = makeBlock(LF_BlockWoodenTableLow.class, woodentablelowblockid, LF_TileEntityWoodenTableLow.class, Material.wood, 2.0f, "LF_Block_WoodenTableLow");
		itemWoodenTableLow = makeItem(woodentablelowitemid, blockWoodenTableLow, "LF_WoodenTableLow.png", "LF_Item_WoodenTableLow");

		blockWoodenTable1x1 = makeBlock(LF_BlockWoodenTable1x1.class, woodentable1x1blockid, LF_TileEntityWoodenTable1x1.class, Material.wood, 2.0f, "LF_Block_WoodenTable1x1");
		itemWoodenTable1x1 = makeItem(woodentable1x1itemid, blockWoodenTable1x1, "LF_WoodenTable1x1.png", "LF_Item_WoodenTable1x1");

		blockSofa = makeBlock(LF_BlockSofa.class, sofablockid, LF_TileEntitySofa.class, Material.cloth, 2.0f, "LF_Block_Sofa");
		itemSofa = makeItem(sofaitemid, blockSofa, "LF_Sofa.png", "LF_Item_Sofa");

		blockBench = makeBlock(LF_BlockBench.class, benchblockid, LF_TileEntityBench.class, Material.wood, 2.0f, "LF_Block_Bench");
		itemBench = makeItem(benchitemid, blockBench, "LF_Bench.png", "LF_Item_Bench");

		blockTVOff = makeBlock(LF_BlockTVOff.class, tvoffblockid, LF_TileEntityTV.class, Material.wood, 2.0f, "LF_Block_TVOff");
		blockTVOn = makeBlock(LF_BlockTVOn.class, tvonblockid, LF_TileEntityTV.class, Material.wood, 2.0f, "LF_Block_TVOn");
		itemTV = makeItem(tvitemid, blockTVOff, "LF_TV.png", "LF_Item_TV");

		itemChairTest = makeItem(softchairtestid, "LF_SoftChair.png", "LF_Item_ChairTest");
	}

	public static void debugWrite(String string)
	{
		try
		{
			FileWriter fstream = new FileWriter("C:/Documents and Settings/Owner/AppData/Roaming/.minecraft/mods/DebugOut.txt");
			BufferedWriter out = new BufferedWriter(fstream);
			out.append(string);
			out.close();
		}
		catch(Exception e)
		{
			System.err.println(e);
		}
	}

	public static Object getPrivateField(Class class1, Object object, String strField)
	{
		try
		{
			Field field1 = class1.getDeclaredField(strField);
			field1.setAccessible(true);
			return field1.get(object);
		}
		catch(Exception e)
		{
			System.err.println("LF ERROR - Failed to retrieve private field");
			System.err.println(e);
		}
		return null;
	}

	public static void setPrivateField(Class class1, Object object, String strField, Object value)
	{
		try
		{
			Field field1 = class1.getDeclaredField(strField);
			field1.setAccessible(true);
			field1.set(object, value);
		}
		catch(Exception e)
		{
			System.err.println("LF ERROR - Failed to retrieve private field");
			System.err.println(e);
		}
	}

	public static Object invokePrivateMethod(Class class1, Object object, String strMethod, Class[] argTypes, Object[] args)
	{
		try
		{
			Method method1 = class1.getDeclaredMethod(strMethod, argTypes);
			method1.setAccessible(true);
			return method1.invoke(object,args);
			
		}
		catch(Exception e)
		{
			System.err.println("LF ERROR - Failed to invoke private method");
			System.err.println(e);
		}
		return null;
	}

	// Blocks/Items
	public static Block blockSoftChair;
	public static Item itemSoftChair;
	public static Block blockWoodenChair;
	public static Item itemWoodenChair;
	public static Block blockWoodenTableStand;
	public static Item itemWoodenTableStand;
	public static Block blockWoodenTable2x1;
	public static Item itemWoodenTable2x1;
	public static Block blockWoodenTable2x2;
	public static Item itemWoodenTable2x2;
	public static Block blockWoodenTableHigh;
	public static Item itemWoodenTableHigh;
	public static Block blockWoodenChairHigh;
	public static Item itemWoodenChairHigh;
	public static Block blockWoodenTableLow;
	public static Item itemWoodenTableLow;
	public static Block blockWoodenTable1x1;
	public static Item itemWoodenTable1x1;
	public static Block blockSofa;
	public static Item itemSofa;
	public static Block blockBench;
	public static Item itemBench;
	public static Block blockTVOn;
	public static Block blockTVOff;
	public static Item itemTV;
	public static Item itemChairTest;
}
